package v2

import (
	"douyin/controller/api/response"
	"douyin/service"
	"log"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

type relationController struct{}

// Action 关注操作，关注、取关
func (r *relationController) Action(c *gin.Context) {
	//取参数
	//actionType, _ := strconv.Atoi(c.Query("action_type"))
	actionType := c.Query("action_type")
	toUserId, _ := strconv.ParseInt(c.Query("to_user_id"), 10, 64)
	//userId, _ := strconv.Atoi(c.Query("user_id"))
	userId, ok := c.Keys["user_id"].(int64)
	if !ok {
		c.JSON(http.StatusOK, response.InvalidParma)
		return
	}
	if actionType == "1" {
		res, err := service.Follow(toUserId, userId)
		if err != nil {
			c.JSON(http.StatusOK, res)
			return
		}
		log.Println("成功关注")
		c.JSON(http.StatusOK, res)
	} else {
		res, err := service.Unfollow(toUserId, userId)
		if err != nil {
			c.JSON(http.StatusOK, res)
			return
		}
		log.Println("成功取关")
		c.JSON(http.StatusOK, res)
	}
}

// FollowList  用户的关注列表
func (r *relationController) FollowList(c *gin.Context) {
	// 获取 user_id
	userId, ok := c.Keys["user_id"].(int64)
	if !ok {
		c.JSON(http.StatusOK, response.InvalidParma)
		return
	}

	res, err := service.QueryFollow(userId)
	if err != nil {
		c.JSON(http.StatusOK, res.Status)
		return
	}
	c.JSON(http.StatusOK, res)
}

// FollowerList 用户的粉丝列表
func (r *relationController) FollowerList(c *gin.Context) {

	userId, ok := c.Keys["user_id"].(int64)
	if !ok {
		c.JSON(http.StatusOK, response.InvalidParma)
		return
	}
	res, err := service.QueryFollowers(userId)
	if err != nil {
		c.JSON(http.StatusOK, res.Status) //出现错误、只需返回状态信息即可
		return
	}
	c.JSON(http.StatusOK, res)
}
